package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import server.FirewallSession;
import server.RulesTableType;

public abstract class AbstractRulesFrame extends JFrame
{
	/**
	 * server controller
	 */
	protected FirewallSession UI;
	
	/* C'tor */
	public AbstractRulesFrame(FirewallSession ui)
	{	
		super("Rules");
		UI = ui;
		try {
			Settings.ruleset = UI.getCurrentRuleSet();
				
			
			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			setSize(Globals.rulesFrameSize);
			setLayout(new BorderLayout());
			
			/*Standart buttons for the frame*/
			addStandartButtons();
			
			Box upButtons = getUpRulesButtons();
			Box downButtons = getDownRulesButtons();
			if((upButtons != null) && (downButtons!= null))
			{
				JPanel b = new JPanel(new GridLayout(1,2));
				b.add(upButtons);
				b.add(downButtons);
				add(b, BorderLayout.NORTH);
			}
			
			JPanel tables = getRulesTables();
			if(tables != null)
			{
				add(tables, BorderLayout.CENTER);
			}
		} catch (RemoteException e) {
			ErrorFrame.AddError("Can't create rules table frame :"+ e.getMessage(), this);
		}
	}

	private void addStandartButtons()
	{
		Box tableBox = Box.createHorizontalBox();
		
		JButton finish = new JButton("Finish");
		finish.setToolTipText("Close and save changes");
		JButton cancel = new JButton("Cancel");
		cancel.setToolTipText("Close without saving changes");
		JButton help = new JButton("Help");
		cancel.setToolTipText("Help for rules screen");
		
		tableBox.add(help);		
		tableBox.add(finish);
		tableBox.add(cancel);
	    
		
		/*Help button listener - open the relevant help */		
		help.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				rulesHelp();
			}	  
		});
		
		/*Finish button listener - closes the frame*/		
		finish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				dispose();				
			}	  
		});
		  
		/*Cancel button listener - closes the frame*/	
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				dispose();			
			}	  
		});

	    add(tableBox, BorderLayout.SOUTH);
	}

	/**
	 * Adds the two rules tables
	 */
	protected JPanel getRulesTables()
	{
		/* Types of tables to create */
		RulesTableType[] tableChoice = {RulesTableType.IN, RulesTableType.OUT}; 
		String[] tableLocation = {BorderLayout.NORTH, BorderLayout.SOUTH}; 
		
		/* Common panel */
		JPanel tables = new JPanel(new BorderLayout());
		
		for(int i = 0; i < tableChoice.length; i++)
		{
			/* Panel for rules */
			JPanel table = new JPanel(new BorderLayout());
			
			/* Table */
			AbstractRulesTable rules = getTable(tableChoice[i]);
			
			/* Label adding */
			JLabel label = new JLabel(rules.getType() + " rules:");
			table.add(label, BorderLayout.NORTH);
			
			/* Header adding */
			table.add(rules.getRulesTableHeader(), BorderLayout.CENTER);
			rules.getTableHeader().setReorderingAllowed(false);	
			
			/* Table adding */
			JScrollPane scrollpane = new JScrollPane(rules);
			scrollpane.getViewport().setPreferredSize(new Dimension(200,230));
			table.add(scrollpane,  BorderLayout.SOUTH);
			
			tables.add(table, tableLocation[i]);
		}

		return tables;
	}
	
	/**
	 * @param table in/out type
	 * @return table of rules
	 */
	protected abstract AbstractRulesTable getTable(RulesTableType type);
	
	protected abstract Box getUpRulesButtons();
	protected abstract Box getDownRulesButtons();
	protected abstract void rulesHelp();
}
